package org.example.service.impl;

import java.sql.Timestamp;
import java.util.List;
import org.example.dto.RestResult;
import org.example.entity.UserLogDO;
import org.example.query.UserLogQuery;
import org.example.repository.UserLogRepository;
import org.example.service.UserLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户操作日志服务实现类
 */
@Service
public class UserLogServiceImpl implements UserLogService {
    private static final Logger logger = LoggerFactory.getLogger(UserLogServiceImpl.class);

    @Autowired
    private UserLogRepository userLogRepository;

    @Override
    @Transactional
    public RestResult<Void> recordRegisterLog(Integer userId, String details) {
        UserLogDO log = new UserLogDO();
        log.setUserId(userId);
        log.setOperationType(UserLogDO.OperationType.register);
        log.setOperationTime(new Timestamp(System.currentTimeMillis()));
        log.setDetails(details);
        log.setCreateBy("system");
        log.setCreateTime(new Timestamp(System.currentTimeMillis()));
        log.setUpdateBy("system");
        log.setUpdateTime(new Timestamp(System.currentTimeMillis()));

        userLogRepository.save(log);
        logger.info("记录用户注册日志: userId={}, details={}", userId, details);
        return RestResult.success(null);
    }

    @Override
    @Transactional
    public RestResult<Void> recordLoginLog(Integer userId, String ipAddress, String userAgent, String details) {
        UserLogDO log = new UserLogDO();
        log.setUserId(userId);
        log.setOperationType(UserLogDO.OperationType.login);
        log.setOperationTime(new Timestamp(System.currentTimeMillis()));
        log.setDetails(details);
        log.setCreateBy("system");
        log.setCreateTime(new Timestamp(System.currentTimeMillis()));
        log.setUpdateBy("system");
        log.setUpdateTime(new Timestamp(System.currentTimeMillis()));

        userLogRepository.save(log);
        logger.info("记录用户登录日志: userId={}, ipAddress={}, userAgent={}, details={}", userId, ipAddress, userAgent, details);
        return RestResult.success(null);
    }

    @Override
    @Transactional
    public RestResult<Void> recordLogoutLog(Integer userId, String details) {
        UserLogDO log = new UserLogDO();
        log.setUserId(userId);
        log.setOperationType(UserLogDO.OperationType.logout);
        log.setOperationTime(new Timestamp(System.currentTimeMillis()));
        log.setDetails(details);
        log.setCreateBy("system");
        log.setCreateTime(new Timestamp(System.currentTimeMillis()));
        log.setUpdateBy("system");
        log.setUpdateTime(new Timestamp(System.currentTimeMillis()));

        userLogRepository.save(log);
        logger.info("记录用户登出日志: userId={}, details={}", userId, details);
        return RestResult.success(null);
    }

    @Override
    public RestResult<List<UserLogDO>> queryUserLogs(UserLogQuery query) {
        List<UserLogDO> logs;
        if (query.getUserId() != null && query.getOperationType() != null && query.getStartTime() != null && query.getEndTime() != null) {
            logs = userLogRepository.findByUserIdAndOperationTypeAndOperationTimeBetween(
                    query.getUserId(), 
                    UserLogDO.OperationType.valueOf(query.getOperationType()), 
                    query.getStartTime(), 
                    query.getEndTime());
        } else if (query.getUserId() != null && query.getStartTime() != null && query.getEndTime() != null) {
            logs = userLogRepository.findByUserIdAndOperationTimeBetween(
                    query.getUserId(), 
                    query.getStartTime(), 
                    query.getEndTime());
        } else if (query.getOperationType() != null && query.getStartTime() != null && query.getEndTime() != null) {
            logs = userLogRepository.findByOperationTypeAndOperationTimeBetween(
                    UserLogDO.OperationType.valueOf(query.getOperationType()), 
                    query.getStartTime(), 
                    query.getEndTime());
        } else if (query.getStartTime() != null && query.getEndTime() != null) {
            logs = userLogRepository.findByOperationTimeBetween(
                    query.getStartTime(), 
                    query.getEndTime());
        } else {
            logs = userLogRepository.findAll();
        }

        logger.info("查询用户操作日志: query={}", query);
        return RestResult.success(logs);
    }
}